package com.parsechina.hpush.broker.spi.persistence.redis;

import com.parsechina.hpush.broker.config.HPushConfig;
import com.parsechina.hpush.broker.internal.subscriptions.SubscriptionsStore;
import com.parsechina.hpush.broker.spi.*;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Redis main persistence implementation
 *
 * @author linfeng-eqxiu
 */
public class RedisPersistentStore implements IPersistentStore {

    private static final Logger LOG = LoggerFactory.getLogger(RedisPersistentStore.class);

    private RedissonClient redissonClient;
    private RedisMessagesStore messagesStore;
    private RedisSessionsStore sessionsStore;
    private RedisClientStore clientStore;

    public RedisPersistentStore(HPushConfig props, RedissonClient redissonClient) {

        this.messagesStore = new RedisMessagesStore(redissonClient);
        this.messagesStore.initStore();

        this.sessionsStore = new RedisSessionsStore(redissonClient, messagesStore);
        this.sessionsStore.initStore();

        this.clientStore = new RedisClientStore(redissonClient);

        this.redissonClient = redissonClient;

    }

    /**
     * Factory method to create message store backed by MapDB
     */
    @Override
    public IMessagesStore messagesStore() {
        return messagesStore;
    }

    @Override
    public ISessionsStore sessionsStore() {
        return sessionsStore;
    }

    @Override
    public IAuthenticator authenticatorStore() {
        return null;
    }

    @Override
    public SubscriptionsStore subscriptionsStore() {
        return new SubscriptionsStore(sessionsStore());
    }

    @Override
    public IClientStore clientStore() {
        return null;
    }

    @Override
    public void close() {
        LOG.debug("closed disk storage");
    }

}
